<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/10/14
 * Time: 11:49
 * editor: zhouzhou
 */

namespace app\index\domain;

use app\admin\api\ConfigApi;
use app\index\domain\BaseDomain;
class ConfigDomain extends BaseDomain{
    protected $business_code = "config";
    // public function force_refresh(){
    //     $notes = "客户端" . $this->client_id . "，调用APP配置强制刷新接口";
    //     addLog("Config/force_refresh", $_GET, $_POST, $notes);

    //     $group = 6;//6是接口参数
    //     $result = apiCall(ConfigApi::GET_INFO,array(array('name'=>'REFRESH','group'=>$group)));

    //     if(!$result['status']){
    //         $this->apiReturnErr($result['info']);
    //     }
    //     $config = $result['info'];
    //     if(empty($config)){
    //         $this->apiReturnSuc(array('name'=>'REFRESH','value'=>'0'));
    //     }

    //     $this->apiReturnSuc(array('name'=>$config['name'],'value'=>$config['value']));
    // }

    public function app(){
        $this->checkVersion(102);
        $notes = "客户端" . $this->client_id . "，调用APP配置查询接口";
        addLog("Config/app", $_GET, $_POST, $notes);

        $group  = 6;//6是接口参数
        $result = apiCall(ConfigApi::QUERY_NO_PAGING,[['group'=>$group]]);
        // $t = $this->simpleResult($result['info']);
        // print_r($t);exit;
        if(false === $result['status']) $this->apiReturnErr($result['info']);
        else{
            $result = $this->simpleResult($result['info']);
            unset($result['ios_update_log']);
            unset($result['android_update_log']);
            $this->apiReturnSuc($result);
        }

    }

    public function version(){
        $this->checkVersion(102);
        $notes = "客户端" . $this->client_id . "，调用APP版本查询接口";
        addLog("Config/version", $_GET, $_POST, $notes);
        $r = [];
        $from  = $this->_post("app_type","","缺少来源");
        $result = apiCall(ConfigApi::QUERY_NO_PAGING,[['group'=>6]]);
        if(false === $result['status']) $this->apiReturnErr($result['info']);
        $info = $this->simpleResult($result['info']);
        // $this->apiReturnSuc($info);
        if(strcasecmp($from, 'ios') == 0){
            $r['version']      = $info['ios_version'];
            $r['download_url'] = $info['ios_download_url'];
            $r['update_log']   = $info['ios_update_log'];
        }else if(strcasecmp($from, 'android') == 0){
            $r['version']      = $info['android_version'];
            $r['download_url'] = $info['android_download_url'];
            $r['update_log']   = $info['android_update_log'];
        }
        $this->apiReturnSuc($r);
    }

    private function simpleResult($result){
        $result = is_array($result)?$result:[];
        $simpleResult = [];
        foreach($result as $vo){
            $val  = $vo['value'];
            $name = strtolower($vo['name']);
            if($vo['type'] == 3){
                $val = str_replace('&amp;', '&', $val);
                $array = preg_split('/[,;\r\n]+/', trim($val, ",;\r\n"));
                if (strpos($val, ':')) {
                    $val = [];
                    foreach ($array as $va) {
                        list($k, $v) = explode(':', $va,2);
                        if(in_array($name,['customer_phone','ios_download_url','android_download_url'])){
                            $v = explode('--', $v,2);
                            // if(count($v)<2) array_unshift($v,'');
                            if(count($v)<2){
                                $v = ['name'=>''.$k,'value' =>$v[0]];
                            }else{
                                $v = ['name'=>$v[0],'value' =>$v[1]];
                            }
                        }
                        $val[] = $v;
                    }
                } else {
                    $val = $array;
                }
            }else{
                if($vo['type'] == 1 && in_array($name,['ios_download_url','android_download_url'])){
                    $val = [['name'=>'','value'=>$val]];
                }else{
                    $val = htmlspecialchars_decode($val);
                }
            }
            $simpleResult[$name] = $val;
        }
        return $simpleResult;
    }

}